/**
 *
 */
package co.fitstart.payment.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericJdbcSupport;

import co.fitstart.payment.entity.request.RequestRecord;
import co.fitstart.payment.service.RequestRecordService;

/**
 * @author lute
 *
 */
@Service("requestRecordService")
public class RequestRecordServiceImpl implements RequestRecordService {

    @Autowired
    private GenericJdbcSupport genericJdbcSupport;

    /* (non-Javadoc)
     * @see cn.xcm.payment.service.RequestRecordService#create(cn.xcm.payment.entity.request.RequestRecord)
     */
    public void create(RequestRecord requestRecord) {
        String sentence = "INSERT INTO request_record (app_id, channel_id, request_no, order_no, customer_id, notify_url, request_time, is_disabled) VALUES (:appId, :channelId, :requestNo, :orderNo, :customerId, :notifyUrl, :requestTime, :disabled)";
        genericJdbcSupport.executeSQL(sentence, requestRecord);
    }

    /* (non-Javadoc)
     * @see cn.xcm.payment.service.RequestRecordService#getRequestRecord(java.lang.String)
     */
    public RequestRecord getRequestRecord(String requestNo) {
        String sentence = "SELECT id AS id, app_id AS app_id, channel_id AS channel_id, request_no AS request_no, order_no AS order_no, customer_id AS customer_id, notify_url AS notify_url, request_time AS request_time, is_disabled AS disabled FROM request_record WHERE request_no = :requestNo";
        List<RequestRecord> result = genericJdbcSupport.queryForList(sentence, "requestNo", requestNo, RequestRecord.class);
        return CollectionUtils.isNotEmpty(result) ? result.get(0) : null;
    }

    /* (non-Javadoc)
     * @see co.fitstart.payment.service.RequestRecordService#getRequestRecordsOrderedByDescendingTime(java.lang.String)
     */
    public List<RequestRecord> getRequestRecordsOrderedByDescendingTime(String orderNo) {
        String sentence = "SELECT id AS id, app_id AS app_id, channel_id AS channel_id, request_no AS request_no, order_no AS order_no, customer_id AS customer_id, notify_url AS notify_url, request_time AS request_time, is_disabled AS disabled FROM request_record WHERE order_no = :orderNo ORDER BY request_time DESC";
        return genericJdbcSupport.queryForList(sentence, "orderNo", orderNo, RequestRecord.class);
    }

}
